package cn.zjweu.service.impl;

import cn.zjweu.mapper.CommentMapper;
import cn.zjweu.mapper.UserMapper;
import cn.zjweu.pojo.Comment;
import cn.zjweu.pojo.User;
import cn.zjweu.service.CommentService;
import cn.zjweu.utils.BeanCopyUtils;
import cn.zjweu.utils.ResultUtils;
import cn.zjweu.utils.ResultVo;
import cn.zjweu.vo.CommentVo;
import cn.zjweu.vo.PageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultVo commentList(String commentType, Long menuId, Integer pageNum, Integer pageSize) {

//        //获取该菜品的全部根评论
//        LambdaQueryWrapper<Comment> parentWrapper = new LambdaQueryWrapper<>();
//        //评论类型
//        parentWrapper.eq(Comment::getType, commentType);
//        //判断是否是菜品评论
//        parentWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType), Comment::getArticleId, articleId);
//        parentWrapper.orderByDesc(Comment::getCreateTime);
//        parentWrapper.eq(Comment::getRootId, SystemConstants.ROOT_ARTICLE);
//        Page<Comment> page = new Page<>(pageNum, pageSize);
//
//        page(page, parentWrapper);
//        List<Comment> rootComments = page.getRecords();

        PageHelper.startPage(pageNum, pageSize);
        List<Comment> rootComments = commentMapper.getAllRootComments(commentType, menuId);

        List<CommentVo> rootCommentVos = toCommentList(rootComments);
        rootCommentVos.stream()
                .map(commentVo -> commentVo.setChildren(getChildren(commentVo.getId())))
                .collect(Collectors.toList());
        return ResultUtils.success("获取成功", 200, new PageVo(rootCommentVos, ((Page) rootComments).getTotal()));
    }

    private List<CommentVo> toCommentList(List<Comment> comments) {

        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(comments, CommentVo.class);
        commentVos.stream()
                .map(commentVo -> {
                    User user = userMapper.getById(commentVo.getCreateBy());
                    if (commentVo.getToCommentUserId() >= 1) {
                        commentVo.setToCommentUserName(userMapper.getById(commentVo.getToCommentUserId()).getUsername());
                    }
                        commentVo.setAvatar(user.getAvatar());
                        commentVo.setUsername(user.getUsername());
                    return commentVo;
                })
                .collect(Collectors.toList());

        return commentVos;
    }

    public List<CommentVo> getChildren(Long id) {

        List<Comment> sonComments = commentMapper.getChildren(id);
//        LambdaQueryWrapper<Comment> sonWrapper = new LambdaQueryWrapper<>();
//        sonWrapper.eq(Comment::getRootId, id);
//        sonWrapper.orderByAsc(Comment::getCreateTime);
//        List<Comment> sonComments = list(sonWrapper);

        return toCommentList(sonComments);
    }
}
